package yxy.game.pm2.command.dungeon;

import org.joda.time.DateTime;
import yxy.cherry.battle.module.record.result.BattleResult;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_冲榜;
import yxy.game.pm2.bean.module.rank.active.RankRushEnum;
import yxy.game.pm2.bean.module.rank.active.RankRushHandle;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.DungeonMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.battle.BattleModule;
import yxy.game.pm2.module.dungeon.DungeonHandle;
import yxy.game.pm2.module.task.TaskHandle;

/** 日常副本 副本挑战结算 */
@MessageType(proto = Command.DungeonChanllengeBattleClear, classtype = DungeonMessage.DungeonChanllengeBattleClearRequest.class)
public class CMD_DungeonChanllengeBattleClear extends Command<DungeonMessage.DungeonChanllengeBattleClearRequest> {

	@Override
	public void execute(MessageSource messageSource, DungeonMessage.DungeonChanllengeBattleClearRequest request) {
		String playerCode = messageSource.getTag();
		String battleCode = request.getBattleCode();
//		BattleMessage.BattleType battleType = request.getBattletype();

		DungeonMessage.DungeonChanllengeBattleClearResponse.Builder response = DungeonMessage.DungeonChanllengeBattleClearResponse.newBuilder();

		BattleModule battleModule = BattleModule.get(playerCode, battleCode);
		if (battleModule.isSettled()) {
			send(messageSource, response, Result.BattleHasSettled);// 战斗已结算
			return;
		}
		if (!battleModule.isFinished()) {
			send(messageSource, response, Result.BattleIsNotFinished);// 战斗尚未结束
			return;
		}

		DungeonHandle handle = new DungeonHandle();
		if (handle.isBattling(battleModule)) {
			if (!battleModule.isSkip()) {
				send(messageSource, response, Result.BattleIsNotFinished);// 战斗尚未结束
				return;
			}
		}

		response.setInfo(new BattleHandle().battleClear(battleModule));
		battleModule.setSettled(true);

		BattleResult battleResult = battleModule.getBattleResult();
		if (!battleResult.equals(BattleResult.LEFT_WIN)) {
			battleModule.set(DungeonHandle.battleExpireDay);
			send(messageSource, response, Result.Success);
			return;
		}

		battleModule.getSettlement().settle();
		battleModule.set(DungeonHandle.battleExpireDay);

		response.addAllRewards(new CurrencyHandle().packItems(battleModule.getSettlement().reward()));

		send(messageSource, response, Result.Success);

		TaskHandle.handle(playerCode, TaskType.统计型_副本关卡_次数, 1);
		//todo 每次循环的前半小时内不开启榜
		ActiveModule_冲榜 am = ActiveModule_冲榜.get(playerCode, false);
		DateTime begin = am.getRunend().minusDays(6).withHourOfDay(0).withMinuteOfHour(30).withSecondOfMinute(0).withMillisOfSecond(0);
		if (!DateTime.now().isBefore(begin)) {
			RankRushHandle.addRankRushData(playerCode, 1, RankRushEnum.冲榜_副本关卡.getType());

		}
	}

	private void send(MessageSource messageSource, DungeonMessage.DungeonChanllengeBattleClearResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}